package com.bw.common.aop

import android.util.Log
import android.view.View
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut

/**
 * @author:created by hahajing
 * 时间：2024/4/28 0028
 * 企鹅号:444511958
 * 描述:
 */
@Aspect
class ClickAspect {
    @Pointcut("execution(* android.view.View.OnClickListener.onClick(..))")
    fun clickMethod() {
    }

    @Around("clickMethod()")
    @Throws(Throwable::class)
    fun clickMethodAround(joinPoint: ProceedingJoinPoint) {
        //获取被拦截到的方法的参数列表
        val args = joinPoint.args
        var view: View? = null
        for (arg in args) {
            if (arg is View) {
                view = arg
            }
        }
        //获取View 的 string id
        var resEntryName: String? = null
        var resName: String? = null
        if (view != null) {
            resEntryName = view.context.resources.getResourceEntryName(view.id)
            resName = view.context.resources.getResourceName(view.id)
        }
        //原方法要正常的执行过去 proceed 类似 反射中的invoke方法
        joinPoint.proceed()
        Log.d("123", "after onclick: 控件名称: $resEntryName  资源ID: $resName")
    }
}
